{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a433a75",
   "metadata": {},
   "source": [
    "### filter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc6c1db7",
   "metadata": {},
   "source": [
    "filter也是接收一个函数和一个序列，filter把传入的函数依次作用于每个元素，根据返回值是True或False决定保留还是丢弃该元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2cf18558",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_odd(n):\n",
    "    return n % 2 == 1\n",
    "list(filter(is_odd, list(range(10))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "117ffd69",
   "metadata": {},
   "source": [
    "filter是一个高阶函数，关键在于正确实现一个筛选函数。\n",
    "另外filter返回的是一个Iterator，是一个惰性序列。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1e2c425",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7774347c",
   "metadata": {},
   "source": [
    "filter使用了惰性计算，所以只有在取filter结果的时候，才会真正筛选并每次返回下一个晒出的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b47e839",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
